<html>
<head><meta charset="utf-8"><title>catch_unwind not working in release? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html">catch_unwind not working in release?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228111745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228111745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228111745">(Feb 28 2021 at 07:05)</a>:</h4>
<p>I'm trying to debug an issue where catch_unwind seems to not work in release (the panic happens but isn't caught, so the program exits). The code base is a translation from C to Rust (using c2rust) and I switched from using setjmp/longjmp to using catch_unwind/panic. Everything works fine in release mode. I'm slowly migrating the unsafe code to using safe alternatives and in this case, the goal is to switch from using function pointers and void* contexts to using closures. The first instance where the tests fail is after applying this commit: <a href="https://gitlab.com/ahmedcharles/lua-rs/-/commit/a0b627cc22675798c4f71c6f807c0250e31611eb">https://gitlab.com/ahmedcharles/lua-rs/-/commit/a0b627cc22675798c4f71c6f807c0250e31611eb</a></p>
<p>Any help (and ideas on how to debug the issue) is greatly appreciated.</p>



<a name="228129886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228129886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228129886">(Feb 28 2021 at 13:11)</a>:</h4>
<p>you can set a breakpoint on a <code>rust_panic</code> and see what is on your callstack.</p>



<a name="228129892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228129892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228129892">(Feb 28 2021 at 13:11)</a>:</h4>
<p>maybe you're panicking elsewhere and there isn't actually a catch_unwind where you think there should be one.</p>



<a name="228129899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228129899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228129899">(Feb 28 2021 at 13:12)</a>:</h4>
<p>(that's my intuition without digging into the code too much)</p>



<a name="228134493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228134493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228134493">(Feb 28 2021 at 14:42)</a>:</h4>
<p><span class="user-mention" data-user-id="336395">@Ahmed Charles</span> I suspect this is because the compiler assumes that <code>extern "C"</code> functions never unwind and therefore the destructors are unreachable and can be removed.</p>



<a name="228141477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228141477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228141477">(Feb 28 2021 at 16:47)</a>:</h4>
<p>did we actually ever implement anything of the sort? IIRC the nounwind for extern "C" functions was backed out because it broke certain high profile bindings relying on setjmp/longjmp.</p>



<a name="228141909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228141909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228141909">(Feb 28 2021 at 16:55)</a>:</h4>
<ul>
<li><a href="https://github.com/rust-lang/rust/blob/1b1d85fd4cc6ace45d5ec95cd95ce2ccbe6f2e17/compiler/rustc_codegen_ssa/src/mir/block.rs#L115">https://github.com/rust-lang/rust/blob/1b1d85fd4cc6ace45d5ec95cd95ce2ccbe6f2e17/compiler/rustc_codegen_ssa/src/mir/block.rs#L115</a></li>
<li><a href="https://github.com/rust-lang/rust/blob/1b1d85fd4cc6ace45d5ec95cd95ce2ccbe6f2e17/compiler/rustc_middle/src/ty/layout.rs#L2587">https://github.com/rust-lang/rust/blob/1b1d85fd4cc6ace45d5ec95cd95ce2ccbe6f2e17/compiler/rustc_middle/src/ty/layout.rs#L2587</a></li>
</ul>



<a name="228192101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228192101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228192101">(Mar 01 2021 at 07:51)</a>:</h4>
<p>Ah, ok. I'll have to remove all <code>extern "C"</code> functions before switching to using <code>catch_unwind</code>... hopefully that works.</p>



<a name="228206997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228206997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228206997">(Mar 01 2021 at 10:23)</a>:</h4>
<p>You could also wait for <a href="https://github.com/rust-lang/rust/issues/76570">#76570</a> to land and use <code>extern "C-unwind"</code>.</p>



<a name="228239664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/catch_unwind%20not%20working%20in%20release%3F/near/228239664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/catch_unwind.20not.20working.20in.20release.3F.html#228239664">(Mar 01 2021 at 14:32)</a>:</h4>
<p>That's fine, I wanted to remove <code>extern "C"</code> anyways, I just didn't know it impacted unwind. The tests pass now, so this was the cause. Thanks.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>